﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Longtop.Web.WebSite.Admin.Roles.CreatePermissionModel>" %>
<%@ Import Namespace="Longtop.Web.WebSite.Models" %>
<ul class="permission">
    <%
        foreach (var item in Model.Permissions.GroupBy(x => x.PermissionName.Remove(x.PermissionName.LastIndexOf(".")))) {
            var index = item.FirstOrDefault(x => x.PermissionName.EndsWith("index", true, null));
            
    %><li>
        <%
            var i = Model.Permissions.IndexOf(index);

            if (index != null) {
        %>
        <input name="Role.Permissions[<%= i%>].LableId" value="<%=index.LableId%>" type="hidden" />
        <label for="<%=index.PermissionName %>">
            <input id="<%=index.PermissionName %>" type="checkbox" name="Role.Permissions[<%=i %>].PermissionName"
                value="<%=index.PermissionName %>,true" <%=Model.Role.Permissions.ToList().Exists(p=>p.PermissionName==index.PermissionName)?"checked='checked'":"" %> />
            <%=index.Description%></label>
        <input name="Role.Permissions[<%= i%>].PermissionName" value="<%=index.PermissionName %>,false"
            type="hidden" />
        <span></span>
        <%}
            else {%>
        <label for="other">
            <input id="other" type="checkbox" />
            其它</label>
        <span></span>
        <%}%>
        <ul>
            <%   foreach (var it in item.Except(new[] { index })) {
                     i = Model.Permissions.IndexOf(it);
            %>
            <li>
                <input name="Role.Permissions[<%= i%>].LableId" value="<%=it.LableId%>" type="hidden" />
                <label for="<%=it.PermissionName %>">
                    <input id="<%=it.PermissionName %>" type="checkbox" name="Role.Permissions[<%=i %>].PermissionName"
                        value="<%=it.PermissionName %>,true" <%=Model.Role.Permissions.ToList().Exists(p=>p.PermissionName==it.PermissionName)?"checked='checked'":"" %> />
                    <%=it.Description%></label>
                <input name="Role.Permissions[<%= i%>].PermissionName" value="<%=it.PermissionName %>,false"
                    type="hidden" />
                <span></span></li>
            <%}%>
        </ul>
    </li>
    <%
        }
    %>
</ul>

<script type="text/javascript">
    $(function() {
        $('ul.permission > li input:checkbox')
        //        .click(function() {
        //            var item = $(this);
        //            if (item.is(':disabled')) {
        //                item.removeAttr('disabled');
        //            }
        //        })
        .change(
        function() {
            var item = $(this);
            if (item.is(':disabled')) {
                item.removeAttr('disabled');
                return;
            }
            var children = item.parents('li').find('ul input:checkbox');
            children.attr('checked', item.is(':checked') ? 'checked' : '');
        }
        )
        .end()
        .find('ul.permission > li ul input:checkbox')
        .change(
        function() {
            var all = $(this).parents('li:first').parents('li:first').find('input:checkbox');
            var children = all.not(':first');
            all.filter(':first').attr('checked', children.is(':checked') ? 'checked' : '');
        }
        ).trigger('change');
    });
</script>

